#include"lexer.hpp"

static int gettok()
{
    static int LastChar;

    //skip whitespace
    while(isspace(LastChar))
    {
        LastChar = getchar();
    }

    if(isalpha(LastChar)) //is a letter from alpha...
    {
        IdentifierStr = LastChar;
        while( isalnum( (LastChar = getchar()) ) ) //while char is num or alpha...
        {
            IdentifierStr += LastChar;
        }


        if(IdentifierStr == "def") return tok_def;

        if(IdentifierStr == "extern") return tok_extern;

        return tok_identifier;

    }

    if(isdigit(LastChar) || LastChar == '.')
    {
        std::string Numstr;
        do
        {
            Numstr += LastChar;
            LastChar = getchar();
        }
        while(isdigit(LastChar) || LastChar == '.');

        NumVal = strtod(Numstr.c_str(), 0); //converts string to double

        return tok_number;
    }

    if(LastChar == '#')
    {
        do
        {
            LastChar = getchar();
        }
        while(LastChar != EOF && LastChar != '\n' && LastChar != '\r');
    }

    if(LastChar != EOF)
    {
        return gettok();
    }

    if(LastChar == EOF)
    {
        return tok_eof;
    }

    int ThisChar = LastChar;
    LastChar = getchar();
    return ThisChar;
}
